import Vue from 'vue';
import VueRouter from 'vue-router';

import { routes } from './routes';
Vue.use(VueRouter);

// 解决路由重复点击报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// replace
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch(err => err);
};

// 开发模式下采用hash模式，生成环境下选择history模式——在history模式下，部署需要服务器配合
const router = new VueRouter({
  mode: process.env.VUE_APP_ROUTE_MODE,
  base: process.env.BASE_URL,
  routes
});

export default router;

